layui.use(['table', 'jquery', 'layer'], function () {
    var table = layui.table,
        layer = layui.layer,
        $ = layui.jquery;

    var tableIns = table.render({
        elem: '#supplierList', //绑定table id
        height: 'full-125',
        url: ctx + '/supplier/list',
        page: true,
        cellMinWidth: 80, // 单元格最小宽度
        toolbar: '#toolbarDemo',
        cols: [[ // 表头
            {type: 'checkbox'},
            {type: "numbers"},
            {title: "名称", field: "supplierName"},
            {title: "地址", field: "supplierAddress"},
            {title: "等级", field: "supplierLevel"},
            {
                title: "制单人", field: "empBean", templet: function (data) {
                    return data.empBean.empName;
                }
            },
            {title: "创建时间", field: "supplierCreateDate"},
            {title: "更新时间", field: "supplierUpdateDate"},
            {title: "操作", templet: "#supplierListBar"}
        ]]
    })
    // 监听头部
    table.on('toolbar(supplierFilter)', function (data) {

        if (data.event == "addSupplier") {
            openAddSupplierDialog();
        } else if (data.event == "link") {
            // 添加联系人页面
            var checkStatus = table.checkStatus(data.config.id);
            openSupplierLinkMan(checkStatus.data);
        } else if (data.event == "materialList") {
            var checkStatus = table.checkStatus(data.config.id)
            openMaterialDialog(checkStatus.data)
        }
    })

    table.on("tool(supplierFilter)", function (data) {
        if (data.event == "edit") {
            openUpDateSupplierDialog(data.data.id);
        }
    })

    function openUpDateSupplierDialog(id) {
        layer.open({
            type: 2,
            title: "供应商---修改供应商",
            content: ctx + "/supplier/toUpdateSupplier?id=" + id,
            area: ['700px', '500px'],
            shadeClose: true,
            maxmin: true
        })

    }

    function openAddSupplierDialog() {
        layer.open({
            type: 2,
            title: "供应商---添加供应商",
            content: ctx + "/supplier/toAddSupplierPage",
            area: ['700px', '500px'],
            shadeClose: true,
            maxmin: true
        })
    }


    function openSupplierLinkMan(data) {

        if (data.length === 0) {
            layer.msg("请选择查看供应商", {icon: 5})
            return;
        } else if (data.length > 1) {
            layer.msg("仅能单个查看", {icon: 5})
            return;
        }
        layer.open({
            type: 2,
            title: "供应商管理--- 联系人",
            area: ['700px', '500px'],
            content: ctx + "/supplier/toLinkManPage?id=" + data[0].id,
            shadeClose: true,//点击遮罩关闭层
            maxmin: true
        })
    }

    function openMaterialDialog(data) {
        if (data.length === 0) {
            layer.msg("请选择查看供应商", {icon: 5})
            return;
        } else if (data.length > 1) {
            layer.msg("仅能单个查看", {icon: 5})
            return;
        }
        layer.open({
            type: 2,
            title: "供应商管理---主要产品",
            area: ['700px', '500px'],
            content: ctx + "/supplier/toMaterialListPage?id=" + data[0].id,
            shadeClose: true,//点击遮罩关闭层
            maxmin: true
        })

    }
})